//
//  RacksCollectionViewCell.m
//  danpin
//
//  Created by chuangjia on 1/12/2015.
//  Copyright © 2015 chuangjia. All rights reserved.
//

#import "RacksCollectionViewCell.h"
#import "viewconfig.h"
#import "TTTAttributedLabel.h"
#import "CalcLabel.h"
#import "UIImageView+WebCache.h"
#import "NSDictionary+ObjectExt.h"
#import "UILabel+StringFrame.h"
#define  cellwidth (DEVICE_WIDTH-PHOTO_TIME_PADDING*3)/2
#define IMAGE_MAX_HEIGHT (DEVICE_WIDTH *2/3)

@interface RacksCollectionViewCell()
{
    
}
@property(nonatomic,strong)UIImageView *imageview;
@property(nonatomic,strong)TTTAttributedLabel *title;
@property(nonatomic,strong)UILabel *price;
@property(nonatomic,strong)UILabel *oprice;
//
@property(nonatomic,strong)UIImageView *nation_flag;
@property(nonatomic,strong)UILabel *nation_text;
@end

@implementation RacksCollectionViewCell
- (id)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor =[UIColor whiteColor];
        _imageview = [[UIImageView alloc] initWithFrame:CGRectZero];
        _imageview.contentMode =UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_imageview];
        //
        _title =[[TTTAttributedLabel alloc] initWithFrame:CGRectMake(0,0, cellwidth-PHOTO_FRAME_WIDTH,0)];
        _title.backgroundColor = [UIColor clearColor];
        _title.textColor = TITLE_COLOR;
        _title.textAlignment = NSTextAlignmentLeft;
        _title.numberOfLines=0;
        _title.font =FONT(12);
        _title.lineBreakMode =  UILineBreakModeWordWrap|UILineBreakModeTailTruncation;
        [self.contentView addSubview:_title];
        //
        _price =[[UILabel alloc] initWithFrame:CGRectZero];
        _price.backgroundColor = [UIColor clearColor];
        _price.textColor = RED_COLOR;
        _price.textAlignment = NSTextAlignmentLeft;
        _price.font =  FONT_BOLD(14);
        _price.numberOfLines=0;
        [self.contentView addSubview:_price];
        //
        _oprice =[[UILabel alloc] initWithFrame:CGRectZero];
        _oprice.backgroundColor = [UIColor clearColor];
        _oprice.textColor = CONTEXT_COLOR;
        _oprice.textAlignment = NSTextAlignmentLeft;
        _oprice.font =  FONT(13);
        _oprice.numberOfLines=0;
        [self.contentView addSubview:_oprice];
        
        //
        _nation_flag = [[UIImageView alloc] initWithFrame:CGRectZero];
        _nation_flag.contentMode=UIViewContentModeScaleAspectFit;
        [self.contentView addSubview:_nation_flag];
        //
        _nation_text =[[UILabel alloc] initWithFrame:CGRectZero];
        _nation_text.backgroundColor = [UIColor clearColor];
        _nation_text.textColor = TITLE_COLOR;
        _nation_text.textAlignment = NSTextAlignmentLeft;
        _nation_text.font =  FONT(10);
        _nation_text.numberOfLines=0;
        [self.contentView addSubview:_nation_text];
        
    }
    return self;
}
-(void)dealloc
{
    self.data=nil;
    self.indexPath=nil;
    self.title=nil;
    self.price=nil;
    for (id subView in self.contentView.subviews) {
        [subView removeFromSuperview];
    }
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

/// 根据数据模型来显示内容
- (void)showInfo:(id)model key:(id)key indexPath:(NSIndexPath *)indexPath
{
    self.key = key;
    self.indexPath=indexPath;
    NSDictionary *dict =(NSDictionary*)model;
    self.data =model;
    CGFloat fTop =PHOTO_TIME_PADDING;
    NSString *imageurl = [dict stringForKey:@"thumbnail" withDefaultValue:@""];
    self.imageview.image =nil;

    if (imageurl.length>0) {
        NSString *ww =[dict stringForKey:@"thumbnail_width" withDefaultValue:@"0"];
        NSString *hh =[dict stringForKey:@"thumbnail_height" withDefaultValue:@"0"];
        CGFloat w =[ww floatValue];
        CGFloat h =[hh floatValue];
        
        //限制过高的图片，图片最大高度是屏幕的2/3，详见 IMAGE_MAX_HEIGHT
        if( h > IMAGE_MAX_HEIGHT)
        {
            float fRatio = IMAGE_MAX_HEIGHT / h;
            h = IMAGE_MAX_HEIGHT;
            w = w * fRatio;
        }
        //限制高度结束
        
        CGFloat H =0;
        if (w>0 && h>0) {
            CGFloat fullW =cellwidth-PHOTO_FRAME_WIDTH;
            CGFloat ratio = fullW / w;
            if (w>cellwidth-PHOTO_FRAME_WIDTH) {
                w = ratio *w;
                H = ratio * h;
            }else if (w==cellwidth-PHOTO_FRAME_WIDTH) {
                H = ratio * h;
            }else {
                H = h;
            }
        }else{
            w=cellwidth-PHOTO_FRAME_WIDTH;
            H=cellwidth-PHOTO_FRAME_WIDTH;
        }
        
        _imageview.frame = CGRectMake((cellwidth-w)/2, fTop, w ,H);
        [_imageview sd_setImageWithURL:[NSURL URLWithString:imageurl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            if (error) {
                _imageview.image=[UIImage imageNamed:@"image.png"] ;
            }
            
        }];

        fTop = fTop +_imageview.frame.size.height+PHOTO_TIME_PADDING;
    }else
    {
        self.imageview.image =nil;
    }
    
    NSString *ttitle =[dict stringForKey:@"post_title" withDefaultValue:@""];
    self.title.text =@"";
    //计算多行数据
    if (ttitle.length>0) {
         __weak typeof (self) wSelf = self;
        [self.title setText:ttitle afterInheritingLabelAttributesAndConfiguringWithBlock:^ NSMutableAttributedString  *(NSMutableAttributedString *attributedString)
         {
             [attributedString addAttribute:NSFontAttributeName value:wSelf.title.font range:NSMakeRange(0,[attributedString length])];
             //设置字体颜色
             [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
             return attributedString;
             
         }];
        float fheight  = [CalcLabel getAttributedStringHeightWidthValue:(cellwidth-PHOTO_FRAME_WIDTH) str:self.title.attributedText];
        self.title.frame =CGRectMake(PHOTO_TIME_PADDING,fTop+PHOTO_TIME_PADDING, cellwidth-PHOTO_FRAME_WIDTH, fheight);
        fTop = fTop+fheight;
    }
    //
    NSString *pricestr =[dict stringForKey:@"price" withDefaultValue:@""];
    self.price.text = @"";
    if (pricestr.length>0) {
        self.price.text =pricestr;
        CGSize psize = [self.price boundingRectWithSize:CGSizeMake(cellwidth-PHOTO_FRAME_WIDTH, 20)];
        self.price.frame =CGRectMake(PHOTO_TIME_PADDING, fTop+PHOTO_TIME_PADDING,psize.width, 20);
        
        fTop = fTop+20+PHOTO_FRAME_WIDTH;
    }
    NSString *opricestr =[dict stringForKey:@"oprice" withDefaultValue:@""];
    self.oprice.text =@"";
    if (opricestr.length>0) {
        NSDictionary *attribtDic = @{NSStrikethroughStyleAttributeName: [NSNumber numberWithInteger:NSUnderlineStyleSingle]};
        NSMutableAttributedString *attribtStr = [[NSMutableAttributedString alloc]initWithString:opricestr attributes:attribtDic];
        [attribtStr addAttribute:(id)kCTForegroundColorAttributeName value:CONTEXT_COLOR range:NSMakeRange(0,[attribtStr length])];
        
        self.oprice.attributedText = attribtStr;
        self.oprice.frame =CGRectMake(self.price.frame.origin.x+self.price.frame.size.width+PHOTO_TIME_PADDING, self.price.frame.origin.y, cellwidth-PHOTO_FRAME_WIDTH, 20);
        attribtStr=nil;
    }
    id shop = [dict objectForKey:@"shop"];
    if (shop && [shop isKindOfClass:[NSDictionary class]]) {
        NSDictionary *shopp = (NSDictionary*)shop;
        NSString *nation_flag = [shopp stringForKey:@"nation_flag"];
        if (nation_flag.length>0) {
            _nation_flag.frame = CGRectMake(PHOTO_TIME_PADDING, fTop+2.5, 15,15);
            [_nation_flag sd_setImageWithURL:[NSURL URLWithString:nation_flag] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                if (error) {
                    _nation_flag.image=[UIImage imageNamed:@"image.png"] ;
                }
                
            }];
        }
        NSString *nation_text = [shopp stringForKey:@"nation_text"];
        if (nation_text.length>0) {
            _nation_text.frame = CGRectMake(PHOTO_TIME_PADDING*2+15, fTop, cellwidth-(PHOTO_TIME_PADDING*2+15)-PHOTO_TIME_PADDING,20);
            _nation_text.text = nation_text;
        }
    }
}
/// 返回Cell高度
+ (CGFloat)returnCellHeight:(id)model
{
    NSDictionary *dict = (NSDictionary*)model;
    CGFloat fTop = PHOTO_FRAME_WIDTH;
    NSString *imageurl = [dict stringForKey:@"thumbnail" withDefaultValue:@""];
    if (imageurl.length>0) {
        NSString *ww =[dict stringForKey:@"thumbnail_width" withDefaultValue:@"0"];
        NSString *hh =[dict stringForKey:@"thumbnail_height" withDefaultValue:@"0"];
        CGFloat w =[ww floatValue];
        CGFloat h =[hh floatValue];
        
        
        //限制过高的图片，图片最大高度是屏幕的2/3，详见 IMAGE_MAX_HEIGHT
        if( h > IMAGE_MAX_HEIGHT)
        {
            float fRatio = IMAGE_MAX_HEIGHT / h;
            h = IMAGE_MAX_HEIGHT;
            w = w * fRatio;
        }
        //限制高度结束
        
        CGFloat H =0;
        if (w>0 && h>0) {
            CGFloat fullW =cellwidth-PHOTO_FRAME_WIDTH;
            CGFloat ratio = fullW / w;
            if (w>cellwidth-PHOTO_FRAME_WIDTH) {
                w = ratio *w;
                H = ratio * h;
            }else if (w==cellwidth-PHOTO_FRAME_WIDTH) {
                H = ratio * h;
            }else {
                H = h;
            }
        }else{
            w=cellwidth-PHOTO_FRAME_WIDTH;
            H=cellwidth-PHOTO_FRAME_WIDTH;
        }
        fTop = fTop+H;
    }
    NSString *ttitle =[dict stringForKey:@"post_title" withDefaultValue:@""];
    //计算多行数据 高度
    if(ttitle.length>0)
    {
        NSMutableAttributedString* attributedString =[[NSMutableAttributedString alloc]initWithString:ttitle];
        [attributedString addAttribute:NSFontAttributeName value:FONT(12) range:NSMakeRange(0,[attributedString length])];
        [attributedString addAttribute:(id)kCTForegroundColorAttributeName value:TITLE_COLOR range:NSMakeRange(0,[attributedString length])];
        float fheight = [CalcLabel getAttributedStringHeightWidthValue:(cellwidth-PHOTO_FRAME_WIDTH) str:attributedString];
        attributedString=nil;
        fTop =fTop+fheight+PHOTO_TIME_PADDING;
    }
    NSString *pricestr =[dict stringForKey:@"price" withDefaultValue:@""];
    if (pricestr.length>0) {
        fTop =fTop+PHOTO_FRAME_WIDTH*2;
    }
    id shop = [dict objectForKey:@"shop"];
    if (shop && [shop isKindOfClass:[NSDictionary class]]) {
        NSDictionary *shopp = (NSDictionary*)shop;
        NSString *nation_flag = [shopp stringForKey:@"nation_flag"];
        NSString *nation_text = [shopp stringForKey:@"nation_text"];
        if (nation_text.length>0||nation_flag.length>0) {
            fTop =fTop +20+PHOTO_FRAME_WIDTH;
        }
        
    }
    return fTop;
}
@end
